<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>layui</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link href="../../../static/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
	<link rel="stylesheet" href="../../../static/vui/css/vui.css">
	<link rel="stylesheet" href="../../../static/css/purchase.css">


	<script type="text/javascript" src="../../../static/js/jquery-1.9.1.min.js"></script>
	<script src="../../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/vui.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/treegrid.js"></script>
	<script type="text/javascript" src="../../../static/js/common.js"></script>
</head>
<body>

<div class="vui-form-top">
	<div id="formArea">
		<table class="from-table">
			<tr>
				<td class="label">
					<label>分类名称</label>
				</td>
				<td>
					<input type="text" autocomplete="off" class="input" name="name"/>
				</td>
				<td>
					<button class="button" id="query"><span>查询</span></button>
				</td>
			</tr>
		</table>
	</div>
</div>
<div id="table"></div>

<script>
var editArea = 	['450px', '400px'];

$(function(){
	$('#create').click(function(){
		app.window(layer,{
			title:'添加客户分类',
			area: editArea,
			content: 'customertype_add.html'
		});
	});

	$('#query').click(function(){
		$('#table').datagrid("reload",app.serializeForm());
	});

	$('#table').treegrid({
		width:700,
		url:app.BASE_API_URL+'customerType/select',
		columns:[
			{field:'name',title:'客户分类名称',width:300},
			{title:'操作',width:200,formatter:function(row){
					return app.getOpt(row.id,'添加同级','warning')+app.getOpt(row.id,'添加下级','green')+app.getOpt(row.id,'编辑','edit')+app.getOpt(row.id,'删除','del');
				}}
		],
		loadSuccess:loadDataSuccess
	});
});

function loadDataSuccess(){
	$('.vui-opt-del').click(function(){

		var isleaf = $(this).parent().parent().parent().attr('isleaf');
		if( isleaf == 0 ){
			app.alert(layer,'有下级不能删除',2);
		}else{
			var id = $(this).data('id');
			layer.confirm('确定要删除选中的记录吗？', {icon: 3, title:'提示'}, function(index){
				app.request({
					url:'customerType/delete',
					data:{id:id,status:0},
					success:function (resp) {
                        $('#table').treegrid("reload",app.serializeForm());
						app.actionSuccess_index(resp,index);
					}
				});

			});
		}
	});

	$('.vui-opt-edit').click(function(){
		var rowEle = $(this).parent().parent().parent();
		var parentRowEle = rowEle.parent().find('.vui-table-row[tid="'+rowEle.attr('pid')+'"]');
		var row = rowEle.data();
		var parentRow = parentRowEle.length>0?parentRowEle.data():{name:''};
		app.window(layer,{
			area: editArea,
			content: 'customertype_add.html?id='+row.id+'&parentId='+row.parentId+'&name='+encodeURIComponent(row.name)+'&parentName='+parentRow.name
		});
	});

	$('.vui-opt-green').click(function(){
		var rowEle = $(this).parent().parent().parent();
		var row = rowEle.data();
		app.window(layer,{
			title:'添加供应商分类',
			area: editArea,
			content: 'customertype_add.html?parentId='+row.id+'&parentName='+encodeURIComponent(row.name)
		});
	});

	$('.vui-opt-warning').click(function(){
		var rowEle = $(this).parent().parent().parent();
		var parentRowEle = rowEle.parent().find('.vui-table-row[tid="'+rowEle.attr('pid')+'"]');
		var parentRow = parentRowEle.data();
		parentRow = parentRow ? parentRow : {id:'',name:''};
		app.window(layer,{
			title:'添加供应商分类',
			area: editArea,
			content: 'customertype_add.html?parentId='+parentRow.id+'&parentName='+encodeURIComponent(parentRow.name)
		});
	});
}
</script>

</body>
</html>